//
//  ChooseButtonView.m
//  DeepBreathing
//
//  Created by rimi on 15/12/30.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "ChooseButtonView.h"
@interface ChooseButtonView()

@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;

@end

@implementation ChooseButtonView

-(instancetype)initWithLeftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle andFrame:(CGRect)frame{
    
    self = [super init];
    if (self) {
        self.frame = frame;
        self.leftLabel.text = leftTitle;
        self.rightLabel.text = rightTitle;
        self.chooseIndex = 0;
    }
    
    return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];
    [self addSubview:self.leftLabel];
    [self addSubview:self.rightLabel];
}
- (void)respondsToButton:(UIButton*)sender
{
    if (sender == self.leftButton) {
        self.chooseIndex = 0;
    }else{
        self.chooseIndex = 1;
    }
}
- (UILabel *)leftLabel{
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 55, 30)];
        _leftLabel.center = CGPointMake(50, CGRectGetHeight(self.frame)/2);
    }
    return _leftLabel;
}

- (UILabel *)rightLabel{
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(105, 0, 55, 30)];
        _rightLabel.center = CGPointMake(135, CGRectGetHeight(self.frame)/2);
    }
    return _rightLabel;
}
- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.frame = CGRectMake(0, 0, 20, 20);
        _leftButton.center = CGPointMake(10, CGRectGetHeight(self.frame)/2);
        [_leftButton setImage:[UIImage imageNamed:@"修改个人信息-未选中"] forState:UIControlStateNormal];
        [self.leftButton setImage:[UIImage imageNamed:@"修改个人信息-选中"] forState:UIControlStateSelected];
        [_leftButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}
- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(75, 0, 20, 20);
        _rightButton.center = CGPointMake(85, CGRectGetHeight(self.frame)/2);
        [_rightButton setImage:[UIImage imageNamed:@"修改个人信息-未选中"] forState:UIControlStateNormal];
        [self.rightButton setImage:[UIImage imageNamed:@"修改个人信息-选中"] forState:UIControlStateSelected];
        [_rightButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (void)setChooseIndex:(NSInteger)chooseIndex
{
    _chooseIndex = chooseIndex;
    if (_chooseIndex == 0) {
        self.leftButton.selected = true;
        self.rightButton.selected = false;
    }else{
        self.leftButton.selected = false;
        self.rightButton.selected = true;
    }
    
}



@end
